function getHeight4(data) { //获取高四位
  let height;
  height = (data >> 4);
  return height;
}

function getLow4(data) { //获取低四位
  let low;
  low = (data & 0x0f);
  return low;
}
/**
 * 十六进制字符串转字节数组
 * 每2个字符串转换
 * 903132333435363738 转为 [-112, 49, 50, 51, 52, 53, 54, 55, 56]
 * @param {String} str 符合16进制字符串
 */
function Str2Bytes(str) {
  var pos = 0;
  var len = str.length;
  if (len % 2 != 0) {
    return null;
  }
  len /= 2;
  var hexA = new Array();
  for (var i = 0; i < len; i++) {
    var s = str.substr(pos, 2);
    var v = parseInt(s, 16);
    if (v >= 127) v = v - 255 - 1
    hexA.push(v);
    pos += 2;
  }
  return hexA;
}
/**
 * 字节数组转十六进制字符串
 * [-112, 49, 50, 51, 52, 53, 54, 55, 56] 转换 903132333435363738
 * @param {Array} arr 符合16进制数组
 */
function Bytes2Str(arr) {
  var str = "";
  for (var i = 0; i < arr.length; i++) {
    var tmp;
    var num = arr[i];
    if (num < 0) {
      //此处填坑，当byte因为符合位导致数值为负时候，需要对数据进行处理
      tmp = (255 + num + 1).toString(16);
    } else {
      tmp = num.toString(16);
    }
    if (tmp.length == 1) {
      tmp = "0" + tmp;
    }
    str += tmp;
  }
  return str;
}
/**
 * 十六进制字符串转数组
 * 1185759ac35a91143f97037002b1a266 转换 ["11", "85", "75", "9a", "c3", "5a", "91", "14", "3f", "97", "03", "70", "02", "b1", "a2", "66"]
 * @param {String} str 十六进制字符串
 */
function Str2Arr(str) {
  var pos = 0;
  var len = str.length;
  if (len % 2 != 0) {
    return null;
  }
  len /= 2;
  var hexA = new Array();
  for (var i = 0; i < len; i++) {
    var s = str.substr(pos, 2);
    hexA.push(s);
    pos += 2;
  }
  return hexA;
}
/**
 * 十六进制数组进行异或
 * @param {Array} arr 十六进制数组
 */
function BytesDes(arr) {
  var des = 0
  for (var i = 0; i < arr.length; i++) {
    des ^= parseInt(arr[i], 16)
  }
  return des.toString(16).toUpperCase();
}
/**
 * 十进制转十六进制
 * 15 转 0F
 * @param {Number} num 十进制数字
 */
function toHex(num) {
  return ("0" + (Number(num).toString(16))).slice(-2).toUpperCase()
}
/**
 * 二进制转十六进制
 * 00001001 转 09
 * @param {String} str 二进制字符串
 */
function binTohex(str) {
  let hex_array = [{
      key: 0,
      val: "0000"
    }, {
      key: 1,
      val: "0001"
    }, {
      key: 2,
      val: "0010"
    }, {
      key: 3,
      val: "0011"
    }, {
      key: 4,
      val: "0100"
    }, {
      key: 5,
      val: "0101"
    }, {
      key: 6,
      val: "0110"
    }, {
      key: 7,
      val: "0111"
    },
    {
      key: 8,
      val: "1000"
    }, {
      key: 9,
      val: "1001"
    }, {
      key: 'a',
      val: "1010"
    }, {
      key: 'b',
      val: "1011"
    }, {
      key: 'c',
      val: "1100"
    }, {
      key: 'd',
      val: "1101"
    }, {
      key: 'e',
      val: "1110"
    }, {
      key: 'f',
      val: "1111"
    }
  ]
  let value = ''
  let list = []
  if (str.length % 4 !== 0) {
    let a = "0000"
    let b = a.substring(0, 4 - str.length % 4)
    str = b.concat(str)
  }
  while (str.length > 4) {
    list.push(str.substring(0, 4))
    str = str.substring(4);
  }
  list.push(str)
  for (let i = 0; i < list.length; i++) {
    for (let j = 0; j < hex_array.length; j++) {
      if (list[i] == hex_array[j].val) {
        value = value.concat(hex_array[j].key)
        break
      }
    }
  }
  return value
}

/**
 * 十六进制字符串转ASCLL编码
 * 3132333435363738 转换 12345678
 * @param {String} str 十六进制字符串
 */
function HexToAscll(str) {
  var pos = 0;
  var len = str.length / 2;
  var hexA = '';
  for (var i = 0; i < len; i++) {
    var s = str.substr(pos, 2);
    hexA += String.fromCharCode(`0x${s}`);
    pos += 2;
  }
  return hexA;
}

/**
 * CRC16-CCITT-FALSE算法js版本
 * @param {*} str 
 */
function crc16_false(str) {
  var crc = 0xFFFF;
  for (let s in str) {
    if (!(s % 2)) {
      let oneByte_16 = parseInt(`${str[s]}${str[s*1+1]}`, 16)
      crc ^= oneByte_16 << 8;
      for (var j = 0; j < 8; j++) {
        crc = (crc & 0x8000) ? ((crc << 1) ^ 0x1021) : (crc << 1);
      }
    }
  }
  crc &= 0xffff;
  return Bytes2Str(int22Byte(crc,0,true));
}

/**
 * 将字符转为数组
 * @param {*} str 
 */
function byteArray2IntArray(data) {
  var ints = Array(data.length);
  for (var i = 0; i < data.length; i++) {
    ints[i] = data[i];
  }
  return ints;
}
/**
 * int转byte数组
 *
 * @param num
 * @return bytes
 * flag  true  大端 false  小端
 */
function int24Byte(num, flag) {
  var bytes = Array(4);
  if (flag) {
    bytes[0] = (num & 0xff); //8位
    bytes[1] = ((num >> 8) & 0xff); //16位
    bytes[2] = ((num >> 16) & 0xff);
    bytes[3] = ((num >> 24) & 0xff); //32位
  } else {
    bytes[0] = ((num >> 24) & 0xff);
    bytes[1] = ((num >> 16) & 0xff);
    bytes[2] = ((num >> 8) & 0xff);
    bytes[3] = (num & 0xff);
  }
  return bytes;
}
/**
 * int转byte数组
 *
 * @param num 需要转换的数据
 * @param hl 取低八位或者高八位
 * @return bytes
 * flag  true  大端 false  小端
 */
function int22Byte(num, hl, flag) {
  var bytes = Array(2);
  //Int8Array
  if (hl == 0) {
    if (flag) {
      bytes[1] = (num & 0xff); //8位
      bytes[0] = ((num >> 8) & 0xff); //16位
    } else {
      bytes[0] = (num & 0xff); //8位
      bytes[1] = ((num >> 8) & 0xff); //16位
    }
  } else if (hl == 1) {
    if (flag) {
      bytes[1] = ((num >> 24) & 0xff);
      bytes[0] = ((num >> 16) & 0xff);
    } else {
      bytes[0] = ((num >> 24) & 0xff);
      bytes[1] = ((num >> 16) & 0xff);
    }
  }
  return bytes;
}
//16转10进制带符号
function hex2int(hexStr) {
  let twoStr = parseInt(hexStr,16).toString(2); // 将十六转十进制，再转2进制
  let bitNum = hexStr.length * 4; // 1个字节 = 8bit ，0xff 一个 "f"就是4位
  if(twoStr.length < bitNum){
    while(twoStr.length < bitNum){
      twoStr = "0" + twoStr;
    }
  }
  if(twoStr.substring(0,1) == "0"){
    // 正数
    twoStr = parseInt(twoStr,2); // 二进制转十进制
    return twoStr;
  }else{
    // 负数
    let twoStr_unsign = "";
    twoStr = parseInt(twoStr,2) - 1; // 补码：(负数)反码+1，符号位不变；相对十进制来说也是 +1，但这里是负数，+1就是绝对值数据-1
    twoStr = twoStr.toString(2);
    twoStr_unsign = twoStr.substring(1,bitNum); // 舍弃首位(符号位)
    // 去除首字符，将0转为1，将1转为0   反码
    twoStr_unsign = twoStr_unsign.replace(/0/g, "z");
    twoStr_unsign = twoStr_unsign.replace(/1/g, "0");
    twoStr_unsign = twoStr_unsign.replace(/z/g, "1");
    twoStr = parseInt(-twoStr_unsign, 2);
    return twoStr;
  }
}




export default {
  getHeight4,
  getLow4,
  Str2Bytes,
  Bytes2Str,
  Str2Arr,
  BytesDes,
  toHex,
  binTohex,
  HexToAscll,
  crc16_false,
  int24Byte,
  int22Byte,
  byteArray2IntArray,
  hex2int
};